package com.yx.frame.builder;

import java.util.List;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;

import com.yx.frame.builder.xml.QueryMethodContentXml;
import com.yx.frame.builder.xml.QueryMethodContentXml.QuerySql;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class QueryContent extends Composite {
	Shell window;
	Button pagination;
	private Table sqlTable;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public QueryContent(Composite parent, int style, Shell win) {
		super(parent, style);
		this.window = win;

		pagination = new Button(this, SWT.CHECK);
		pagination.setBounds(10, 12, 84, 17);
		pagination.setText("查询时分页");

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ViewQuerySqlTxt ViewSqlTxt = new ViewQuerySqlTxt(window, SWT.NONE,
						sqlTable,-1);
				ViewSqlTxt.open();
			}
		});
		button.setBounds(438, 7, 46, 27);
		button.setText("添加");

		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int idx = sqlTable.getSelectionIndex();
				if (idx >= 0) {
					sqlTable.remove(idx);
					for (int i = 0; i < sqlTable.getItemCount(); i++) {
						sqlTable.getItem(i).setText(0, "" + (i + 1));
					}
				}
			}
		});
		button_1.setText("删除");
		button_1.setBounds(589, 7, 46, 27);

		sqlTable = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		sqlTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int idx = sqlTable.getSelectionIndex();
				if (idx >= 0 && idx < sqlTable.getItemCount()) {
					ViewQuerySqlTxt ViewSqlTxt = new ViewQuerySqlTxt(window, SWT.NONE,
							sqlTable,idx);
					ViewSqlTxt.open();
				}
			}
		});
		sqlTable.setBounds(10, 38, 626, 347);
		sqlTable.setHeaderVisible(true);
		sqlTable.setLinesVisible(true);
		TableColumn tc1 = new TableColumn(sqlTable, SWT.CENTER);
		TableColumn tc2 = new TableColumn(sqlTable, SWT.LEFT);
		TableColumn tc3 = new TableColumn(sqlTable, SWT.LEFT);
		TableColumn tc4 = new TableColumn(sqlTable, SWT.LEFT);
		TableColumn tc5 = new TableColumn(sqlTable, SWT.LEFT);
		TableColumn tc6 = new TableColumn(sqlTable, SWT.LEFT);
		TableColumn tc7 = new TableColumn(sqlTable, SWT.LEFT);
		tc1.setText("序号");
		tc2.setText("名称");
		tc3.setText("查询语句");
		tc4.setText("解压列");
		tc5.setText("数据源");
		tc6.setText("自定义数据源");
		tc7.setText("列名");
		tc1.setWidth(45);
		tc2.setWidth(80);
		tc3.setWidth(220);
		tc4.setWidth(60);
		tc5.setWidth(80);
		tc6.setWidth(130);
		tc7.setWidth(130);
		
		Button button_2 = new Button(this, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=sqlTable.getSelectionIndex();
				if(sqlTable.getItemCount()<1)return;
				if(selectIdx<0||selectIdx>=sqlTable.getItemCount()-1){
					return ;
				}
				TableItem selectItem=sqlTable.getItem(selectIdx);
				TableItem nextItem=sqlTable.getItem(selectIdx+1);
				String tmp;
				for(int i=0;i<7;i++){
					tmp=selectItem.getText(i);
					selectItem.setText(i, nextItem.getText(i));
					nextItem.setText(i, tmp);
				}
				for (int i = 0; i < sqlTable.getItemCount(); i++) {
					sqlTable.getItem(i).setText(0, "" + (i + 1));
				}
				sqlTable.setSelection(selectIdx+1);
			}
		});
		button_2.setText("下移");
		button_2.setBounds(488, 7, 46, 27);
		
		Button button_3 = new Button(this, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=sqlTable.getSelectionIndex();
				if(sqlTable.getItemCount()<1)return;
				if(selectIdx<1||selectIdx>=sqlTable.getItemCount()){
					return ;
				}
				TableItem selectItem=sqlTable.getItem(selectIdx);
				TableItem upItem=sqlTable.getItem(selectIdx-1);
				String tmp;
				for(int i=0;i<7;i++){
					tmp=selectItem.getText(i);
					selectItem.setText(i, upItem.getText(i));
					upItem.setText(i, tmp);
				}
				for (int i = 0; i < sqlTable.getItemCount(); i++) {
					sqlTable.getItem(i).setText(0, "" + (i + 1));
				}
				sqlTable.setSelection(selectIdx-1);
			}
		});
		button_3.setText("上移");
		button_3.setBounds(538, 7, 46, 27);

	}

	public void setQueryMethodContentXml(
			QueryMethodContentXml queryMethodContentXml) {
		List<QuerySql> list = queryMethodContentXml.getTransaction();
		for (int i = 0; i < list.size(); i++) {
			QuerySql querySql = list.get(i);
			TableItem item1 = new TableItem(sqlTable, SWT.NONE);
			item1.setText(new String[] { "" + querySql.getIndex(),querySql.getName(),
					querySql.getSql(),querySql.getUnzipcolumn(),querySql.getDatasourceStr(),querySql.getUserDefineDatasource(),querySql.getSqlColums() });
		}
		for (int i = 0; i < sqlTable.getItemCount(); i++) {
			sqlTable.getItem(i).setText(0, "" + (i + 1));
		}
		//callbackMethod.setText(queryMethodContentXml.getCallback());
	}

	public QueryMethodContentXml getQueryMethodContentXml() {
		QueryMethodContentXml queryMethodContentXml = new QueryMethodContentXml();
		for (int i = 0; i < sqlTable.getItemCount(); i++) {
			TableItem item = sqlTable.getItem(i);
			queryMethodContentXml.addQuerySql(
					Integer.parseInt(item.getText(0)), item.getText(1), item.getText(2), item.getText(3),item.getText(4),item.getText(5),item.getText(6));
		}
		queryMethodContentXml.setCallback("");
		return queryMethodContentXml;
	}

	public void setQueryInfo(String paginationStr) {
		if ("true".equals(paginationStr)) {
			pagination.setSelection(true);
		} else {
			pagination.setSelection(false);
		}
		
	}

	public String getPagination() {
		if (pagination.getSelection()) {
			return "true";
		} else {
			return "false";
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
